package com.sm.appbase.base.basemvp;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.Nullable;


import com.gyf.immersionbar.ImmersionBar;
import com.shuoyue.appbase.R;
import com.sm.appbase.base.BaseActivity;
import com.sm.appbase.net.exception.NetErrorException;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * @author azheng
 * @date 2018/4/24.
 * GitHub：https://github.com/RookieExaminer
 * Email：wei.azheng@foxmail.com
 * Description：
 */
public abstract class BaseMvpActivity<T extends BasePresenter> extends BaseActivity implements BaseView {

    protected T mPresenter;
    SpotsDialog spotsDialog;
    protected CompositeDisposable mCompositeDisposable;
    protected boolean isUseSelfImmersionBar = false;//是否自行定制状态栏，在super.oncreate前指定
    protected boolean isHiddSoftInput = true;//



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        mCompositeDisposable = new CompositeDisposable();
        super.onCreate(savedInstanceState);
        if (isHiddSoftInput)
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        if (!isUseSelfImmersionBar) {
            ImmersionBar.with(this)
                .statusBarAlpha(1f)  //状态栏透明度，不写默认0.0f
                    .statusBarColor(R.color.alph)     //状态栏颜色，不写默认透明色
//                    .autoStatusBarDarkModeEnable(true, 0.2f) //自动状态栏字体变色，必须指定状态栏颜色才可以自动变色哦
                    .statusBarDarkFont(false, 1f)
                    .init();
        }
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); //隐藏状态栏
    }

    @Override
    protected void onPause() {
        Log.e("act:" + this.getClass().getName(), "onPause");
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        if (mCompositeDisposable != null) {
            mCompositeDisposable.clear(); // clear时网络请求会随即cancel
            mCompositeDisposable = null;
        }
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        Log.e("act:" + this.getClass().getName(), "destroy");
        super.onDestroy();
    }


    @Override
    public void bindDispos(Disposable disposable) {
        if (mCompositeDisposable == null || mCompositeDisposable.isDisposed()) {
            mCompositeDisposable = new CompositeDisposable();
        }
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void showLoading(String title) {
        if (spotsDialog == null) {
            spotsDialog = new SpotsDialog(mContext, title);
        }
        if (!TextUtils.isEmpty(title)) {
            spotsDialog.setText(title);
        } else {
            spotsDialog.setText("");
        }
        if (!spotsDialog.isShowing()) {
            spotsDialog.show();
        }
    }

    @Override
    public void showLoading() {
        showLoading(null);
    }

    @Override
    public void hideLoading() {
        if (spotsDialog != null && spotsDialog.isShowing()) {
            spotsDialog.dismiss();
        }

    }

    @Override
    public void onError(NetErrorException throwable) {

    }

    @Override
    public void toast(String msg) {
        if (!TextUtils.isEmpty(msg))
            Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }
}
